import Cookies from 'js-cookie'

const TokenKey = 'vue_admin_template_token'
const userKey = 'username'

export function getToken() {
  return Cookies.get(TokenKey)
}

export function setToken(token) {
  return Cookies.set(TokenKey, token)
}

export function removeToken() {
  return Cookies.remove(TokenKey)
}

export function getUsername () { 
  return localStorage.getItem(userKey)
}

export function flattenAsyncRouter (asyncRouterMap) {
  // 扁平化异步路由表
    let arr = []
  
    function flattenFun (router) {
      router.forEach(item => {
        if (item.name !== '') {
          arr.push(item.name)
        }
        if (item.child && item.child.length !== 0) {
          flattenFun(item.child)
        }
      })
    }
  
    flattenFun(asyncRouterMap)
    return arr
  }